﻿CREATE FUNCTION [dbo].[udf_SplitApplications]
(
	@Applications varchar(2000)
)
RETURNS 
@ParsedList table
(
	[Application] varchar(2000)
)
AS
BEGIN
	DECLARE @Application varchar(256), @Pos int

	SET @Applications = LTRIM(RTRIM(@Applications))+ ','
	SET @Pos = CHARINDEX(',', @Applications, 1)

	IF REPLACE(@Applications, ',', '') <> ''
	BEGIN
		WHILE @Pos > 0
		BEGIN
			SET @Application = LTRIM(RTRIM(LEFT(@Applications, @Pos - 1)))
			IF @Application <> ''
			BEGIN
				INSERT INTO @ParsedList ([Application]) 
				VALUES (@Application)
			END
			SET @Applications = RIGHT(@Applications, LEN(@Applications) - @Pos)
			SET @Pos = CHARINDEX(',', @Applications, 1)

		END
	END	
	RETURN
END